import request from "@/utils/request";
import {QueueScheduleModel} from "@/model/QueueScheduleModel";

export default class ScheduleApi {

    public static downloadTemplate(): Promise<void> {
        return new Promise((resolve, reject) => {
            request({
                url: `/queue/schedule/download/template`,
                responseType: 'blob',
                method: 'get',
                headers: {
                    "Content-Type": "application/x-www-form-urlencoded",
                },
            }).then((res: any) => {
                let b = new Blob([res]);     //拿到文件流下载对象
                let url = window.URL.createObjectURL(b);  //生成文件流下载链接
                let a = document.createElement('a');      //创建一个a标签用来跳转
                a.href = url;                             // a标签 href  赋值 链接
                a.style.display = 'none'
                a.download = '排班导入模板.xlsx';                  //设置下载文件的文件名和文件格式
                document.body.appendChild(a);             //将标签DOM，放置页面
                a.click();
                window.URL.revokeObjectURL(url);     //释放 url 对象内存
                document.body.removeChild(a);//移除标签节点
                resolve()
            }, () => reject())
        })
    }

    /**
     * 查询配置列表
     *
     * @param deptId
     */
    public static listScheduleByDeptId(deptId: string = null, itemId: string = null): Promise<QueueScheduleModel[]> {
        return new Promise((resolve, reject) => {
            request({
                url: `/queue/schedule/list`,
                method: 'get',
                params: {
                    deptId: deptId,
                    itemId: itemId
                }
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(res.data.map(item => new QueueScheduleModel().deserialize(item)))
                }
                reject(res);
            })
        })
    }

    public static saveEmployeeSchedule(schedules: QueueScheduleModel[]): Promise<QueueScheduleModel[]> {
        return new Promise((resolve, reject) => {
            request({
                url: `/queue/schedule/save`,
                method: 'Post',
                data: schedules
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(res.data.map(item => new QueueScheduleModel().deserialize(item)))
                } else {
                    reject(res)
                }
            }, ()=> {reject()})
        })
    }

}

